package org.example.dao;

import org.apache.ibatis.annotations.*;
import org.example.domain.Dept;

import java.util.List;

/**
 * @author Jimmy
 * @version 1.0
 */
public interface DeptDao {

    /**
     * 分页查询（含条件）
     * @param dept dept
     * @return list
     */
    @Select("<script>select dept_id deptId,dept_name deptname,parent_id pid ,state from ss_dept " +
            "where 1=1 " +
            "<if test= 'deptId != null '> and dept_id like concat(concat('%',#{deptId}),'%') </if>" +
            "<if test= 'deptname != null '> and dept_name like concat(concat('%',#{deptname}),'%') </if>" +
            "<if test= 'pid != null '> and parent_id like concat(concat('%',#{pid}),'%') </if>" +
            "</script>")
    @ResultType(Dept.class)
    List<Dept> findByPage(Dept dept);

    /**
     * 查询总条目数(含条件)
     * @param dept dept
     * @return int
     */
    @Select("<script>select count(*) from ss_dept " +
            "where 1=1" +
            "<if test= 'deptId != null '> and dept_id like concat(concat('%',#{deptId}),'%') </if>" +
            "<if test= 'deptname != null '> and dept_name like concat(concat('%',#{deptname}),'%') </if>" +
            "<if test= 'pid != null '> and parent_id like concat(concat('%',#{pid}),'%') </if>" +
            "</script>")
    int findTotal(Dept dept);

    /**
     * 根据id查询dept
     * @param deptId 部门id
     * @return dept
     */
    @Select("select dept_id deptId,dept_name deptname,parent_id pid ,state from ss_dept where dept_id=#{deptId}")
    @ResultType(Dept.class)
    Dept findById(String deptId);

    /**
     * 添加一个部门
     * @param dept dept
     * @return int
     */
    @Insert("insert into ss_dept values(#{deptId},#{deptname},#{pid},#{state})")
    int addDept(Dept dept);

    /**
     * 修改部门信息
     * @param dept dept
     * @return int
     */
    @Update("<script> update ss_dept " +
            "<set>" +
            "   <if test='deptname != null'> dept_name=#{deptname}, </if>" +
            "   <if test='pid != pid'> parent_id=#{pid}, </if>" +
            "   <if test='state != null'> state=#{state} </if>" +
            "</set>" +
            " where dept_id=#{deptId}" +
            "</script>")
    int update(Dept dept);

    /**
     * 删除部门信息
     * @param deptId 部门id
     * @return int
     */
    @Delete("delete from ss_dept where dept_id=#{deptId}")
    int delete(String deptId);

}
